//
//  HHisExamContentViewController.m
//  NursingManages
//
//  Created by Zhen Huang on 17/9/30.
//  Copyright © 2017年 huangzhen. All rights reserved.
//

#import "HHisExamContentViewController.h"

#import "HChooseBankViewController.h"
#import "DIHTest.h"
#import "HAnswerTableViewCell.h"
#import "HExamHeadView.h"
#import "HtestCollectionViewCell.h"
#import "SizeToFit.h"
#import <TMCache.h>

#import "HSingleSeletView.h"
#import "HHisSingleSeletView.h"
#import "HHisMultiSelectView.h"
#import "HMultiSelectView.h"
#import "UIButton+BackgroundColor.h"

#import "Header.h"
#import "HCommon.h"

#import "HShowALLQuesView.h"
#import "Model_Hr_ExamContent.h"
#import "Networking.h"

#import <MSWeakTimer.h>
#import "FLCountDownView.h"
#import "Model_Hr_Exam_Primary.h"
#import "InitDbTableExamOnLine.h"
#import "UserDefault.h"
#import "ModelToJson.h"
@interface HHisExamContentViewController ()
<UICollectionViewDelegateFlowLayout, UICollectionViewDataSource>


@property (strong, nonatomic) UICollectionViewFlowLayout *collectionViewLayout;

@property (strong, nonatomic) UICollectionView           *collectionView;

//数据源
@property (strong, nonatomic) NSMutableArray             *dataSource;
//答案
@property (strong, nonatomic) NSMutableArray             *resultArray;

@property (strong, nonatomic) NSMutableArray             *indexPathArray;
//当前选中的item
@property (assign, nonatomic) NSInteger                   currentSelectIndex;

@property (nonatomic, strong) NSMutableArray *array;

@property (nonatomic, strong) UIButton *button;

@property (nonatomic, copy) NSString *exam_id;

@property (nonatomic, strong)UILabel *timeLabel;

@property (nonatomic, strong)NSMutableArray *onLineArray;

@property (nonatomic, copy) NSString *nowTime;

@property (nonatomic, copy) NSString *res_id;

@property (nonatomic, assign) NSInteger result_flag;

@property (nonatomic, assign) NSInteger record_flag;

@property (nonatomic, assign) NSInteger type;

@end

@implementation HHisExamContentViewController


- (instancetype)initWithDataArray:(NSString *)exam_id
                           res_id:(NSString *)res_id
                      result_flag:(NSInteger)result_flag
                      record_flag:(NSInteger)record_flag
                             type:(NSInteger)type{
    if (self) {
        self.exam_id = exam_id;
        self.array = [NSMutableArray new];
        self.res_id = res_id;
        self.dataSource = [NSMutableArray new];
        self.onLineArray = [NSMutableArray new];
        self.record_flag = record_flag;
        self.result_flag = result_flag;
        self.type = type;
    }
    return self;
}



- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    self.nowTime =  [[HBaseViewController alloc]getNowDate];
    
    
   self.dataSource = [[InitDbTableExamOnLine alloc]GetDetailIUnUploadTableWithResid:self.res_id];
    
    
    [self initView];
    
    
}


- (void)initView {
    
    
    
    [self.view addSubview:self.collectionView];
    
    
}

- (void)initData {
    
    [self dataSource];
    [self resultArray];
    [self indexPathArray];
    
    
    for (int i = 0; i < _dataSource.count; i++) {
        
        [_resultArray addObject:@[]];
        [_indexPathArray addObject:@[]];
    }
    
    
}

// 设置单元格的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}

// 设置段数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"UICollectionViewCell" forIndexPath:indexPath];
    
    if (cell) {
        
        for (UIView *view in cell.contentView.subviews) {
            
            if (view) {
                
                [view removeFromSuperview];
            }
        }
    }
    
    
    Model_Hr_ExamAnswer *model = self.dataSource [indexPath.row];
    
    if ([model.TYPEID isEqualToString:@"1"] || [model.TYPEID isEqualToString:@"3"] || [model.TYPEID isEqualToString:@"4"]) {
        
        NSString *string  = [NSString stringWithFormat:@"%@",model.TITLE];
        CGFloat height = [SizeToFit getHeightByWidth:ScreenWidth - 20 title:string font:[UIFont systemFontOfSize:19]];
        
        
        __weak typeof(self) weakSelf = self;
        
        HHisSingleSeletView *singleView = [[HHisSingleSeletView alloc]initWithconfigure:indexPath.row dataArray:self.dataSource headHeight:height + 20 headTitle:string typeid:model.TYPEID onlineArray:self.onLineArray res_id:self.res_id FromType:1 examDetail:self.dataSource];
        
        singleView.frame = cell.contentView.bounds;
        [cell.contentView addSubview:singleView];
        
        
        singleView.SingleSelectBlock = ^(NSIndexPath *seleIndexPath, NSInteger btnTag ,BOOL isright ,NSInteger state) {
            
            __strong typeof(weakSelf)strongSelf = weakSelf;
            if (indexPath.item+1<self.dataSource.count) {
                if (state == 1 ) {
                    [strongSelf.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:indexPath.item + 1 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
                }else {
                    [strongSelf.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:btnTag inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
                }
                
            }else {
                [HCommon popupErrorMessage:@"已经是最后一题了"];
            }
        };
        
    }else {
        
        NSString *string  = [NSString stringWithFormat:@"%@",model.TITLE];
        CGFloat height = [SizeToFit getHeightByWidth:ScreenWidth - 20 title:string font:[UIFont systemFontOfSize:19]];
        
        HHisMultiSelectView *multiView = [[HHisMultiSelectView alloc]initWithconfigure:indexPath.row dataArray:self.dataSource headHeight:height + 20 headTitle:string typeid:model.TYPEID onlineArra:self.onLineArray res_id:self.res_id examDetail:self.dataSource];
        multiView.frame = cell.contentView.bounds;
        
        [cell.contentView addSubview:multiView];
        
        __weak typeof(self) weakSelf = self;
        
        multiView.MultiSelectBlock = ^(NSMutableArray * btnTagSum,NSInteger state,NSInteger rows) {
            
            if (indexPath.item+1<self.dataSource.count) {
                if (state == 1) {
                    [weakSelf.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:indexPath.item + 1 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
                }else {
                    [weakSelf.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:rows inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
                }
                
            }else {
                
                [HCommon popupErrorMessage:@"已经是最后一题了"];
            }
            
        };
    }
    return cell;
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    
    [self scrollViewEndScrolling:scrollView.contentOffset];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    [self scrollViewEndScrolling:scrollView.contentOffset];
    
}

// 设置单元格大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake(ScreenWidth, ScreenHeight - 64 );
}

// cell与cell之间的间隔，边距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    //上左下右
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

// 设置垂直最小间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    
    return 0;
}

// 设置水平最小间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    
    return 0;
}

// 设置标题头大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    
    return CGSizeMake(0, 0);
}

- (void)scrollViewEndScrolling:(CGPoint)contentOffset {
    
    NSInteger index = contentOffset.x / ScreenWidth;
    
    
    
    _currentSelectIndex = index;
}

//上一题下一题
- (void)collectionViewScrollToItem:(BOOL)bl btnTag:(NSInteger)btnTag {
    
    
    
    [_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:_currentSelectIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
    
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        _collectionViewLayout                          = [[UICollectionViewFlowLayout alloc] init]; // 自定义的布局对象
        _collectionView                                = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64 , ScreenWidth, ScreenHeight - 64 ) collectionViewLayout:_collectionViewLayout];
        //移动方向
        _collectionViewLayout.scrollDirection          = UICollectionViewScrollDirectionHorizontal;
        //是否显示
        _collectionView.showsHorizontalScrollIndicator = NO;
        //是否有反弹效果
        _collectionView.bounces                        = YES;
        //是否整页翻页
        _collectionView.pagingEnabled                  = YES;
        //数据源代理
        _collectionView.dataSource                     = self;
        _collectionView.delegate                       = self;
        //滚动返回至顶部
        _collectionView.scrollsToTop                   = NO;
        _collectionView.backgroundColor                = [UIColor whiteColor];
        // 注册cell
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];
    }
    return _collectionView;
}



- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [[NSMutableArray alloc]init];
    }
    return _dataSource;
}

- (NSMutableArray *)resultArray {
    if (!_resultArray) {
        _resultArray = [[NSMutableArray alloc]init];
    }
    return _resultArray;
}

- (NSMutableArray *)indexPathArray {
    if (!_indexPathArray) {
        _indexPathArray = [[NSMutableArray alloc]init];
    }
    return _indexPathArray;
}

@end
